﻿@inject ILogger<Delete> Logger
@inject IJSRuntime JSRuntime
@inject IRoleManagementService RoleManagementService

@inherits BlazorAdmin.Helpers.BlazorComponent

@namespace BlazorAdmin.Pages.RolePage

@using BlazorAdmin.Interfaces
@using BlazorAdmin.Models
@using Microsoft.AspNetCore.Identity


<div class="modal @_modalClass" tabindex="-1" role="dialog" style="display:@_modalDisplay">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            @if (_item is null || _item.Role is null)
            {
                <Spinner></Spinner>
            }
            else
            {
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Delete @_item.Role.Name</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close" @onclick="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="container">
                        <div class="row">
                            <p>
                                Are you sure you want to <strong class="text-danger">DELETE</strong> the <strong>@_item.Role.Name</strong> role?
                            </p>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal" @onclick="Close">Cancel</button>
                    <button class="btn btn-danger" @onclick="() => DeleteClick(_item.Role.Id)">
                        Delete
                    </button>
                </div>
            }
        </div>
    </div>
</div>

@if (_showDeleteModal)
{
    <div class="modal-backdrop fade show"></div>
}

@code {
    [Parameter]
    public EventCallback<string> OnSaveClick { get; set; }

    private string _modalDisplay = "none;";
    private string _modalClass = "";
    private bool _showDeleteModal = false;
    private GetByIdRoleResponse _item = new GetByIdRoleResponse();

    private async Task DeleteClick(string id)
    {
        await RoleManagementService.Delete(id);

        Logger.LogInformation("Deleted Role Id: {id}", id);
        await OnSaveClick.InvokeAsync(null);
        await Close();
    }

    public async Task Open(string id)
    {
        await new Css(JSRuntime).HideBodyOverflow();

        _item = await RoleManagementService.GetById(id);
        Logger.LogInformation("Loaded role: {id}", id);

        _modalDisplay = "block;";
        _modalClass = "Show";
        _showDeleteModal = true;

        StateHasChanged();
    }

    private async Task Close()
    {
        await new Css(JSRuntime).ShowBodyOverflow();
        _modalDisplay = "none";
        _modalClass = "";
        _showDeleteModal = false;
    }
}
